package com.example.choupaiapp.main.activity.message;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.adapter.BaseAdapterHelper;
import com.example.choupaiapp.common.adapter.QuickAdapter;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.common.view.XListView;
import com.example.choupaiapp.main.activity.mine.FeedBackActivity;
import com.example.choupaiapp.main.bean.CommonQuestion;
import com.example.choupaiapp.main.constant.Url;
import com.hyphenate.easeui.EaseConstant;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2019/11/18.
 */

public class ChouPaiActivity extends CommonActivity {

    @BindView(R.id.listview)
    XListView listview;
    @BindView(R.id.question_ll)
    LinearLayout question_ll;
    private String userId;
    private QuickAdapter<CommonQuestion.ListBean> quickAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chou_pai);

    }

    @Override
    protected void initView() {

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            userId = bundle.getString("userId");
        }

        quickAdapter = new QuickAdapter<CommonQuestion.ListBean>(context, R.layout.item_question) {
            @Override
            protected void convert(BaseAdapterHelper helper, CommonQuestion.ListBean item) {
                helper.setText(R.id.question_tv, item.getQuestion());
            }
        };

        listview.setAdapter(quickAdapter);

        listview.setOnItemClickListener((adapterView, view, i, l) -> {
            Bundle bundle1 = new Bundle();
            bundle1.putString("url", quickAdapter.getItem(i).getAnswer());
            startActivity(QuestionWebviewActivity.class, bundle1);
        });

    }

    @Override
    protected void initData() {
        showDialog(context, null);
        finalOkGo.request(RequestType.POST, Url.COMMON_QUESTION, null, Url.COMMON_QUESTION, new Callback<CommonQuestion>() {
            @Override
            public void onSuccess(CommonQuestion commonQuestion) {
                dismissDialog();
                if (commonQuestion.getCode() == Contacts.SUCCESS) {
                    quickAdapter.clear();
                    quickAdapter.addAll(commonQuestion.getList());
                    quickAdapter.notifyDataSetChanged();
                } else {
                    if(commonQuestion.getCode() == Contacts.TOKEN_EMPTY){
                    }else{
                        showImageToas(commonQuestion.getMsg());
                    }
                }
            }

            @Override
            public void onError(Response response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

//    @Override
    @OnClick({R.id.detail_more, R.id.service, R.id.question, R.id.feed_back})
    public void onViewClicked(View view) {
//        super.onViewClicked(view);
        if (Utils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.detail_more:
                break;
            case R.id.service:
                Intent intent = new Intent(this, ChatActivity.class);
                intent.putExtra(EaseConstant.EXTRA_USER_ID, userId);
                startActivity(intent);
                break;
            case R.id.question:
                question_ll.setVisibility(View.VISIBLE);
                break;
            case R.id.feed_back:
                Bundle bundle = new Bundle();
                bundle.putString("flag", "feed");
                startActivity(FeedBackActivity.class);
                break;
        }
    }
}
